/**
 * Create by richliu1023
 * @date 16/5/19
 * @email richliu1023@gmail.com
 * @github https://github.com/RichLiu1023
 * @description a simple explanation
 */
class UILoading extends egret.DisplayObjectContainer {
	private textField:egret.TextField;
	private titleField:egret.TextField;
	private loading:egret.MovieClip;
	private loadingSP:egret.Sprite;
	private loadingGroupName:string = '';
	private isLoading:boolean = false;

	public constructor() {
		super();
		this.createView();
		this.addEvent();
	}

	public addEvent():void {
		rm.AppLoaderControl.instance.addEventListener( rm.AppLoaderEvent.LOAD_START, this.onStart, this );
		rm.AppLoaderControl.instance.addEventListener( rm.AppLoaderEvent.LOAD_PROGRESS, this.onProgress, this );
		rm.AppLoaderControl.instance.addEventListener( rm.AppLoaderEvent.LOAD_COMPLETE, this.onComplete, this );
	}

	private onStart( event:egret.Event ):void {
		var gname:string = event.data.groupName;
		if ( this.loadingGroupName == gname )return;
		this.loadingGroupName = gname;
		if ( !this.isLoading ) {
			this.addChild( this.loadingSP );
			this.loading.play( -1 );
		}
		this.textField.text = '0%';
		this.titleField.text = event.data.showTxt;
		this.isLoading = true;
	}

	private onProgress( event:egret.Event ):void {
		var gname:string = event.data.groupName;
		if ( this.loadingGroupName != gname )return;
		this.setProgress( event.data.itemsLoaded, event.data.itemsTotal );
	}

	private onComplete( event:egret.Event ):void {
		var gname:string = event.data.groupName;
		if ( this.loadingGroupName != gname ) return;
		this.isLoading = false;
		if ( this.loadingSP.parent ) {
			this.loading.stop();
			this.loadingSP.parent.removeChild( this.loadingSP );
		}
	}

	private createView():void {
		this.loadingSP = new egret.Sprite();
		var bitd = new egret.Bitmap( RES.getRes( 'lading_bg_png' ) );
		this.loadingSP.addChild( bitd );
		bitd.anchorOffsetX = 70;
		bitd.anchorOffsetY = 70;
		this.loading = new egret.MovieClip( ConfigManager.generateMovieClipData() );
		this.loadingSP.addChild( this.loading );
		this.textField = new egret.TextField();
		this.loadingSP.addChild( this.textField );
		this.textField.width = 300;
		this.textField.height = 20;
		this.textField.textAlign = "center";
		this.textField.anchorOffsetX = 150;
		this.textField.anchorOffsetY = 10;
		this.textField.size = 20;
		this.loading.x = GameConfig.STAGE_W / 2;
		this.loading.y = GameConfig.STAGE_H / 2 + 20;

		bitd.x = this.loading.x + 5;
		bitd.y = this.loading.y - 20;
		this.textField.x = this.loading.x + 5;
		this.textField.y = this.loading.y + 20;

		this.titleField = new egret.TextField();
		this.loadingSP.addChild( this.titleField );
		this.titleField.width = 300;
		this.titleField.height = 30;
		this.titleField.textColor = 0xffffff;
		this.titleField.textAlign = "center";
		this.titleField.anchorOffsetX = 150;
		this.titleField.anchorOffsetY = 15;
		this.titleField.size = 30;
		this.titleField.x = this.loading.x;
		this.titleField.y = this.loading.y - 80;
		this.titleField.text = '正在加载……';

		this.loading.play( -1 );
	}

	private setProgress( current, total ):void {
		this.textField.text = Math.ceil( (current / total) * 100 ) + "%";
	}
}